package LeetCode2.动态规划;

public class LC03 {
    public static void main(String[] args) {
        LC03 lc03 = new LC03();
        System.out.println(lc03.lengthOfLongestSubstring("abcabcbb"));
    }

    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int len = s.length();
        if(len == 0) return 0;
        int[] pos = new int[127]; // 记录上一次出现的位置
        int ans = -1 << 30;
        int start = 0;
        for (int i = 0; i < len; i++) {
            start = Math.max(start, pos[chars[i]]+1);
            ans = Math.max(ans, i + 1 - start);
            pos[chars[i]] = i;
        }
        return ans;

    }
}
